/*
 * TCSS 422 Web Spider Project
 * Group Money: Al McKenzie, Michael Pitts, Taylor Zielske
 */
package control.threads;

import model.URLInputCooker;
import model.URLStore;
import control.data.SearchHopper;

/**
 * This class allocates compute time to getting, buffering and parsing web pages
 * for the web spider program.
 * 
 * @author Michael Pitts
 * @version Oct 31, 2011
 */
public class SingleThreadDirector extends AbstractThreadDirector {
	
	/**
	 * Stores if the thread manager should continue, needs to be kept thread safe.  
	 * True if it should, false otherwise.
	 */
	private boolean my_go;

	/**
	 * Creates a new SingleThreadDirector, which will not create any new threads to
	 * parse data or buffer web pages.
	 * @param the_cooker is the URLInputCooker for this program.
	 * @param the_hopper is the SearchHopper for this program.
	 * @param the_store is the URLStore for this program.
	 * @param the_search_words are the words to search for during the run of this program.
	 */
	public SingleThreadDirector(final URLInputCooker the_cooker,
			final SearchHopper the_hopper, final URLStore the_store,
			final String[] the_search_words) {
		super(the_cooker, the_hopper, the_store, the_search_words);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override public void start() {
		setGo(true);
		while (getGo()) {
			cookOne(); // buffers one url
			parseOne(); // tries to parse one url
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override public void stop() {
		setGo(false);
	}
	
	/**
	 * Sets my go value in a thread safe manner.
	 * @param the_go_value is the new value of go.
	 */
	private synchronized void setGo(final boolean the_go_value) {
		my_go = the_go_value;
	}
	
	/**
	 * Gets the current value of go in a thread safe manner.
	 * @return the current value of go.
	 */
	private synchronized boolean getGo() {
		return my_go;
	}

}
